import sys
import codecs

sys.path.append('./')

import DataProcessing

day_date_r0v, day_open_r0v, day_high_r0v, day_low_r0v, day_close_r0v, day_volume_r0v = DataProcessing.data_processing.split_day_data()

week_date, week_open, week_high, week_low, week_close, week_volume = DataProcessing.data_processing.split_week_data()

# 1. 关于平滑系数
# --------------------------------------------------
# 平滑系数 (Smoothing Parameters) 简称SP。
# SP12 = 2 / (12 + 1)
# SP26 = 2 / (26 + 1)
# --------------------------------------------------
# 2. 关于需求指数
# --------------------------------------------------
# 需求指数 (Demand Index) 用以代替收盘指数
# 不需要特别精细的话，收盘指数可以用收盘价代替。

class ta_index():

    def day_ma():

        day_ma5 = []
        day_ma10 = []
        day_ma20 = []
        day_ma30 = []
        day_ma60 =[]
        day_ma120 = []
        day_ma250 = []

        # 计算5日MA
        for i in range(0,len(day_close_r0v)):

            if i < 4:

                day_ma5.append(None)

            else:

                value = sum(day_close_r0v[(i-4):(i+1)]) / 5

                day_ma5.append(round(value,3))
                
        # 计算10日MA
        for i in range(0,len(day_close_r0v)):

            if i < 9:

                day_ma10.append(None)

            else:

                value = sum(day_close_r0v[(i-9):(i+1)]) / 10

                day_ma10.append(round(value,3))
                
        # 计算20日MA
        for i in range(0,len(day_close_r0v)):

            if i < 19:

                day_ma20.append(None)

            else:

                value = sum(day_close_r0v[(i-19):(i+1)]) / 20

                day_ma20.append(round(value,3))

        # 计算30日MA
        for i in range(0,len(day_close_r0v)):

            if i < 29:

                day_ma30.append(None)

            else:

                value = sum(day_close_r0v[(i-29):(i+1)]) / 30

                day_ma30.append(round(value,3))

        # 计算60日MA
        for i in range(0,len(day_close_r0v)):

            if i < 59:

                day_ma60.append(None)

            else:

                value = sum(day_close_r0v[(i-59):(i+1)]) / 60

                day_ma60.append(round(value,3))

        # 计算120日MA
        for i in range(0,len(day_close_r0v)):

            if i < 119:

                day_ma120.append(None)

            else:

                value = sum(day_close_r0v[(i-119):(i+1)]) / 120

                day_ma120.append(round(value,3))

        # 计算250日MA
        for i in range(0,len(day_close_r0v)):

            if i < 249:

                day_ma250.append(None)

            else:

                value = sum(day_close_r0v[(i-249):(i+1)]) / 250

                day_ma250.append(round(value,3))

        return day_ma5, day_ma10, day_ma20, day_ma30, day_ma60, day_ma120, day_ma250

    def week_ma():

        week_ma5 = []
        week_ma10 = []
        week_ma20 = []
        week_ma30 = []
        week_ma60 =[]
        week_ma120 = []
        week_ma250 = []

        # 计算5周MA
        for i in range(0,len(week_close)):

            if i < 4:

                week_ma5.append(None)

            else:

                value = sum(week_close[(i-4):(i+1)]) / 5

                week_ma5.append(round(value,3))
                
        # 计算10周MA
        for i in range(0,len(week_close)):

            if i < 9:

                week_ma10.append(None)

            else:

                value = sum(week_close[(i-9):(i+1)]) / 10

                week_ma10.append(round(value,3))
                
        # 计算20周MA
        for i in range(0,len(week_close)):

            if i < 19:

                week_ma20.append(None)

            else:

                value = sum(week_close[(i-19):(i+1)]) / 20

                week_ma20.append(round(value,3))

        # 计算30周MA
        for i in range(0,len(week_close)):

            if i < 29:

                week_ma30.append(None)

            else:

                value = sum(week_close[(i-29):(i+1)]) / 30

                week_ma30.append(round(value,3))

        # 计算60周MA
        for i in range(0,len(week_close)):

            if i < 59:

                week_ma60.append(None)

            else:

                value = sum(week_close[(i-59):(i+1)]) / 60

                week_ma60.append(round(value,3))

        # 计算120周MA
        for i in range(0,len(week_close)):

            if i < 119:

                week_ma120.append(None)

            else:

                value = sum(week_close[(i-119):(i+1)]) / 120

                week_ma120.append(round(value,3))

        # 计算250周MA
        for i in range(0,len(week_close)):

            if i < 249:

                week_ma250.append(None)

            else:

                value = sum(week_close[(i-249):(i+1)]) / 250

                week_ma250.append(round(value,3))

        return week_ma5, week_ma10, week_ma20, week_ma30, week_ma60, week_ma120, week_ma250
                
    def day_ema():

        day_ema12 = []
        day_ema26 = []

        for i in range(0,len(day_close_r0v)):

            if i == 0 :

                day_ema12.append(day_close_r0v[i])

                day_ema26.append(day_close_r0v[i])

            else:

                day_ema12_1 = 2/(12+1) * day_close_r0v[i] + (12+1-2)/(12+1) * day_ema12[i-1]

                day_ema26_1 = 2/(26+1) * day_close_r0v[i] + (26+1-2)/(26+1) * day_ema26[i-1]

                day_ema12.append(round(day_ema12_1, 3))

                day_ema26.append(round(day_ema26_1, 3))

        return day_ema12, day_ema26

    def week_ema():

        week_ema12 = []
        week_ema26 = []

        for i in range(0,len(week_close)):

            if i == 0 :

                week_ema12.append(week_close[i])

                week_ema26.append(week_close[i])

            else:

                week_ema12_1 = 2/(12+1) * week_close[i] + (12+1-2)/(12+1) * week_ema12[i-1]

                week_ema26_1 = 2/(26+1) * week_close[i] + (26+1-2)/(26+1) * week_ema26[i-1]

                week_ema12.append(round(week_ema12_1, 3))

                week_ema26.append(round(week_ema26_1, 3))

        return week_ema12, week_ema26

    def day_macd():

        day_ema12, day_ema26 = ta_index.day_ema()

        day_dif = []
        day_dea = []
        day_macd = []

        for i in range(0,len(day_close_r0v)):

            if i == 0 :

                day_dif.append(0)

                day_dea.append(0)

                day_macd.append(0)

            else:

                day_dif_1 = day_ema12[i] - day_ema26[i]

                day_dea_1 = 2/(9+1) * day_dif_1 + (9+1-2)/(9+1) * day_dea[i-1]

                day_macd_1 = (day_dif_1 - day_dea_1) * 2

                day_dif.append(round(day_dif_1, 4))

                day_dea.append(round(day_dea_1, 4))

                day_macd.append(round(day_macd_1, 4))

        return day_dif, day_dea, day_macd

    def week_macd():

        week_ema12, week_ema26 = ta_index.week_ema()

        week_dif = []
        week_dea = []
        week_macd = []

        for i in range(0,len(week_close)):

            if i == 0 :

                week_dif.append(0)

                week_dea.append(0)

                week_macd.append(0)

            else:

                week_dif_1 = week_ema12[i] - week_ema26[i]

                week_dea_1 = 2/(9+1) * week_dif_1 + (9+1-2)/(9+1) * week_dea[i-1]

                week_macd_1 = (week_dif_1 - week_dea_1) * 2

                week_dif.append(round(week_dif_1, 4))

                week_dea.append(round(week_dea_1, 4))

                week_macd.append(round(week_macd_1, 4))

        return week_dif, week_dea, week_macd

# 文件作为脚本直接执行,才执行以下条件.
# import到其他的Python脚本中被调用(模块重用)则不执行以下条件.
if __name__ == '__main__':

    ta_index.day_ma()
    ta_index.day_ema()

# - END -
